package ui.timeline;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import experiment.EventLogger;


public class TimeLine extends JPanel implements ChangeListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7163775881460794525L;
	private TwoThumbTimeLineSlider rangeSlider;
	private OneThumbTimeLineSlider slider;
	public int labelWidth = 100;

	JLabel beginTimeLabel;
	JLabel endTimeLabel;

	JLabel rangeBeginTimeLabel;
	JLabel rangeEndTimeLabel;
	private long beginTime;
	
	public TimeLine(Date currentTime, Date min, Date max)
	{
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		this.beginTime = min.getTime();
		Date beginTime = min;
		Date endTime = max;
		
		Date rangeBeginTime = min;
		Date rangeEndTime = max;
		
	    SimpleDateFormat ft = new SimpleDateFormat ("HH:mm:ss");

		this.rangeBeginTimeLabel = new JLabel(ft.format(rangeBeginTime));
		this.rangeBeginTimeLabel.setPreferredSize(new Dimension(labelWidth, 0));
		this.rangeBeginTimeLabel.setHorizontalAlignment(JLabel.CENTER);
		
		this.rangeSlider = new TwoThumbTimeLineSlider(min.getTime(), max.getTime());
		
		this.rangeEndTimeLabel = new JLabel(ft.format(rangeEndTime));
		this.rangeEndTimeLabel.setPreferredSize(new Dimension(labelWidth,0));
		this.rangeEndTimeLabel.setHorizontalAlignment(JLabel.CENTER);
		
		c.gridy = 0;
		c.gridx = 0;
		c.fill = GridBagConstraints.VERTICAL;
		c.anchor = GridBagConstraints.NORTH;
		this.add(rangeBeginTimeLabel,c);
		c.gridx = 1;
		c.weightx = 1.0f;
		c.fill = GridBagConstraints.BOTH;
		this.add(this.rangeSlider,c);
		c.fill = GridBagConstraints.VERTICAL;
		c.weightx = 0.0f;
		c.gridx = 2;
		this.add(rangeEndTimeLabel,c);
		


		this.beginTimeLabel = new JLabel(ft.format(beginTime));
		this.beginTimeLabel.setPreferredSize(new Dimension(labelWidth, 0));
		this.beginTimeLabel.setHorizontalAlignment(JLabel.CENTER);

		this.slider = new OneThumbTimeLineSlider(min.getTime(), max.getTime());
		
		this.endTimeLabel = new JLabel(ft.format(endTime));
		this.endTimeLabel.setPreferredSize(new Dimension(labelWidth,0));
		this.endTimeLabel.setHorizontalAlignment(JLabel.CENTER);

		
		c.gridy = 1;
		c.gridx = 0;
		c.fill = GridBagConstraints.VERTICAL;
		c.anchor = GridBagConstraints.NORTH;
		this.add(beginTimeLabel,c);
		c.gridx = 1;
		c.weightx = 1.0f;
		c.fill = GridBagConstraints.BOTH;
		this.add(this.slider,c);
		c.fill = GridBagConstraints.VERTICAL;
		c.weightx = 0.0f;
		c.gridx = 2;
		this.add(endTimeLabel,c);
		
		this.rangeSlider.addChangeListener(this);
		this.slider.addChangeListener(this);
	}
	
	public void setCurrentValue(long value) {
		// TODO Auto-generated method stub
		this.slider.setCurrentValue((int)(value - this.slider.getBeginTime()));
	}

	public boolean sliderIsDragging() {
		// TODO Auto-generated method stub
		return this.slider.isDragging();
	}
	
	public boolean rangeSliderIsDragging() {
		// TODO Auto-generated method stub
		return this.rangeSlider.isDragging();
	}

	public long getBeginTime()
	{
		return this.beginTime;
	}

	public long getCurrentValue() {
		// TODO Auto-generated method stub
		return this.slider.getCurrentValue() + this.slider.getBeginTime();
	}

	public long getRangeLowerBound() {
		// TODO Auto-generated method stub
		return this.rangeSlider.getBeginTime() + this.rangeSlider.getLowerValue();
	}

	public long getRangeUpperBound() {
		// TODO Auto-generated method stub
		return this.rangeSlider.getBeginTime() + this.rangeSlider.getUpperValue();
	}
	
	public void setRangeLowerBound(long value)
	{
		this.rangeSlider.setValue((int) value);
		SimpleDateFormat ft = new SimpleDateFormat ("HH:mm:ss");
		this.rangeBeginTimeLabel.setText(ft.format(value));
	}
	public void setRangeUpperValue(long value)
	{
		this.rangeSlider.setUpperValue((int) value);
		SimpleDateFormat ft = new SimpleDateFormat ("HH:mm:ss");
		this.rangeEndTimeLabel.setText(ft.format(value));
	}

	public void addChangeListener(EventLogger eventLogger) {
		// TODO Auto-generated method stub
		this.slider.addChangeListener(eventLogger);
		this.rangeSlider.addChangeListener(eventLogger);

		this.slider.addMouseListener(eventLogger);
		this.rangeSlider.addMouseListener(eventLogger);

		this.slider.addMouseMotionListener(eventLogger);
		this.rangeSlider.addMouseMotionListener(eventLogger);
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource().getClass() == TwoThumbTimeLineSlider.class)
		{
			TwoThumbTimeLineSlider source = (TwoThumbTimeLineSlider) e.getSource();

			SimpleDateFormat ft = new SimpleDateFormat ("HH:mm:ss");

			this.beginTimeLabel.setText(ft.format(new Date(this.beginTime + source.getLowerValue())));
			this.endTimeLabel.setText(ft.format(new Date(this.beginTime + source.getUpperValue())));

			this.slider.setRange(this.beginTime + source.getLowerValue() , this.beginTime + source.getUpperValue());
		}
	}
	
}
